﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tong;
using Tong.TongJS_Prompt;
public partial class aspnet_admin_bock_News_CX_News_Edit : System.Web.UI.Page
{
    /// <summary>
    /// 数据操作通用类 实例 TextData
    /// </summary> 
    public TongUse TextData = new TongUse();


    /// <summary>
    /// 用户存放数据的临时表
    /// </summary>


    /// <summary>
    /// 站点通用方法类
    /// </summary>
    public Tong.ThisSiteUse.SiteTong TextThisSite = new Tong.ThisSiteUse.SiteTong();

    public Tong.ThisSiteUse.CreateSiteHTMlFile CrtFile = new Tong.ThisSiteUse.CreateSiteHTMlFile();

    /// <summary>
    /// id
    /// </summary>
   public string id = "";
    public string Cid = "";

    JsPrompt JsAlert = new JsPrompt();
    protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(aspnet_admin_bock_News_CX_News_Edit));

        DataTable MyDt = new DataTable();
        if (IsPostBack == true)//删除新闻
        {
            if (Request["ChRepeat"] != null)
            {
                TextData.SqlDataSave("delete from CX_NEWS where CX_NEWS_id in(" + Request["ChRepeat"].ToString() + ")");
            }
        }
        if (Request["id"] != null) //此块新加 说明：实现编缉的第二条内容。显示编缉  张立海 2007-10-29  Begin 
        {
            string CX_NEWS_Class_Edit = "cxnet";
            string CX_NEWS_Class_Edit_Source = TextData.SqlataSet("select CX_NEWS_Class_Edit  from [CX_NEWS] where CX_NEWS_id='" + Request["id"].ToString() + "'").Tables[0].Rows[0]["CX_NEWS_Class_Edit"].ToString();
            if (CX_NEWS_Class_Edit_Source.Length < 2 )
            {
                if (Request.Cookies["admin"] != null)
                {
                    CX_NEWS_Class_Edit = CX_NEWS_Creater.Text = TextData.SqlataSet("select * from [Use] where uid='" + Request.Cookies["admin"].Value + "'").Tables[0].Rows[0]["UseName"].ToString();
                }
                TextData.SqlDataSave("update [CX_NEWS] set CX_NEWS_Class_Edit='" + CX_NEWS_Class_Edit + "' where CX_NEWS_id = " + Request["id"].ToString()); //此句说明在用户一打开此页面时就把该用户的名存入 CX_NEWS_Class_Edit　编缉 字段。 
            }

        }

        CX_NEWS_Pic.Attributes.Add("onclick", "showcc()");//添加图片显示事件
        SourceFrom.Attributes.Add("onchange", "document.getElementById('" + CX_NEWS_From.ClientID + "').value=this.value;");
        Authors.Attributes.Add("onchange", "document.getElementById('" + CX_NEWS_Creater.ClientID + "').value=this.value;");
        Keywordssss.Attributes.Add("onchange", "document.getElementById('" + Cx_keyWord_Name.ClientID + "').value=this.value;");
        Button2.Attributes.Add("onclick", "window.open('../../inc/UpFile.aspx?formname=aspnetForm&amp;editname=" + CX_NEWS_Pic_Url.ClientID.ToString() + "&amp;uppath=UpFile','','status=no,scrollbars=no,top=20,left=110,width=600,height=265')");
        if (!IsPostBack)
        {
            PageLand_First();
        }

        if (IsPostBack)
        {
            PageSave();
        }
        
      

    }

    /// <summary>
    /// 加载时出现的东东
    /// </summary>
    protected void PageLand_First()
    {


        try
        {

            DataTable MyDt = new DataTable();

            id = Request.QueryString["id"].Trim();
            int.Parse(id);
            MyDt = TextData.SqlataSet("select * from [CX_NEWS] where CX_NEWS_id=" + id + "").Tables[0];


            #region  病种关键字功能屏蔽 2007-10-11
            //帮关键字
            //帮关键字
            //DataTable TempTab = new DataTable();

            //TempTab = TextData.SqlataSet("select Cx_keyWord_id,Cx_keyWord_Name from [Cx_keyWord]").Tables[0];
            //string tempXtrst = Cx_keyWord_Name.ClientID + "=";
            //for (int i = 0; i < TempTab.Rows.Count; i++)
            //{
            //    tempXtrst += TempTab.Rows[i]["Cx_keyWord_Name"].ToString().Trim().Replace(";", "").Replace("\r", "").Replace("\n", "").Replace(" ", "").Replace("\"", "") + ",";
            //}
            //Labelalert.Text = JsAlert.Input_prompt(tempXtrst);
            //TempTab.Dispose();
            //TempTab.Clear();
            //Keywordssss.DataSource = TextData.SqlataSet("select Cx_keyWord_id,Cx_keyWord_Name from [Cx_keyWord]");
            //Keywordssss.DataTextField = "Cx_keyWord_Name";
            //Keywordssss.DataValueField = "Cx_keyWord_Name";
            //关键字
            //try
            //{
            //    Keywordssss.SelectedValue = MyDt.Rows[0]["Cx_keyWord_Name"].ToString().Trim();
            //    Keywordssss.DataBind();

            //}
            //catch
            //{
            //    Keywordssss.DataBind();

            //}
            #endregion

            //如果新闻分类为空
            try
            {
                News_ClassID.Text = "<select id=\"CX_NEWS_Class_id\"  name=\"CX_NEWS_Class_id\">\r\n" + TextThisSite.selectMueuOption(0, MyDt.Rows[0]["CX_NEWS_Class_id"].ToString().Trim()) + "</select>";


            }
            catch
            {
                News_ClassID.Text = "<select id=\"CX_NEWS_Class_id\"  name=\"CX_NEWS_Class_id\">\r\n" + TextThisSite.selectMueuOption(0, "aa") + "</select>";
                
            }





            // lblRepeat 查重复，如果有重复提示 // lblRepeat


            //  MyDt = TextData.SqlataSet("select * from [CX_NEWS] where CX_NEWS_id=" + id + "").Tables[0];
           // lblRepeat.Text = ShowRepeat(MyDt.Rows[0]["CX_NEWS_Name"].ToString().Trim(), MyDt.Rows[0]["CX_NEWS_id"].ToString());// "<script language='javascript'>alert('该条信用息与数据库中已有的信息重复！！')</script><span id=spRepeat style=background:red;color:white;>重复内容</span>";
            CX_NEWS_Name.Text = MyDt.Rows[0]["CX_NEWS_Name"].ToString().Trim();
            CX_NEWS_Creater.Text = MyDt.Rows[0]["CX_NEWS_Creater"].ToString().Trim();
            CX_NEWS_From.Text = MyDt.Rows[0]["CX_NEWS_From"].ToString().Trim();
            CX_NEWS_Class_Edit.Text = MyDt.Rows[0]["CX_NEWS_Class_Edit"].ToString().Trim();
            //Cx_keyWord_Name.Text = MyDt.Rows[0]["Cx_keyWord_Name"].ToString().Trim();病种关键字功能屏蔽 2007-10-11

            //多选

            CX_NEWS_Top.Value = MyDt.Rows[0]["CX_NEWS_Top"].ToString().Trim();

            if (CX_NEWS_Top.Value == "1")
            {
                CX_NEWS_Top.Checked = true;
            }

            CX_NEWS_Pic.Value = MyDt.Rows[0]["CX_NEWS_Pic"].ToString().Trim();

            if (CX_NEWS_Pic.Value == "1")
            {
                CX_NEWS_Pic.Checked = true;
            }

            CX_NEWS_Pic_Url.Text = MyDt.Rows[0]["CX_NEWS_Pic_Url"].ToString().Trim();
            CX_NEWS_Cont.Html = MyDt.Rows[0]["CX_NEWS_Cont"].ToString().Trim();
            CX_NEWS_Sort.Text = MyDt.Rows[0]["CX_NEWS_Sort"].ToString().Trim();
            CX_NEWS_Description.Text = MyDt.Rows[0]["CX_NEWS_Description"].ToString().Trim();
            CX_NEWS_KeyWords.Text = MyDt.Rows[0]["CX_NEWS_KeyWords"].ToString().Trim();
            CX_NEWS_Title.Text = MyDt.Rows[0]["CX_NEWS_Title"].ToString().Trim();
        }
        catch
        {

            DataTable MyDt = new DataTable();
            MyDt = TextData.SqlataSet("select * from [CX_NEWS]  ").Tables[0];
            try//如果新闻分类为空
            {
                News_ClassID.Text = "<select id=\"CX_NEWS_Class_id\"  name=\"CX_NEWS_Class_id\">\r\n" + TextThisSite.selectMueuOption(0, "aa") + "</select>";
            }
            catch
            { 
                
            }

            CX_NEWS_Name.Text = MyDt.Rows[0]["CX_NEWS_Name"].ToString().Trim();
            CX_NEWS_Creater.Text = MyDt.Rows[0]["CX_NEWS_Creater"].ToString().Trim();
            CX_NEWS_From.Text = MyDt.Rows[0]["CX_NEWS_From"].ToString().Trim();
            //Cx_keyWord_Name.Text = MyDt.Rows[0]["Cx_keyWord_Name"].ToString().Trim(); 病种关键字功能屏蔽 2007-10-11

            //多选

            CX_NEWS_Top.Value = MyDt.Rows[0]["CX_NEWS_Top"].ToString().Trim();

            if (CX_NEWS_Top.Value == "1")
            {
                CX_NEWS_Top.Checked = true;
            }

            CX_NEWS_Pic.Value = MyDt.Rows[0]["CX_NEWS_Pic"].ToString().Trim();

            if (CX_NEWS_Pic.Value == "1")
            {
                CX_NEWS_Pic.Checked = true; 
            }

            CX_NEWS_Pic_Url.Text = MyDt.Rows[0]["CX_NEWS_Pic_Url"].ToString().Trim();
            CX_NEWS_Cont.Html = MyDt.Rows[0]["CX_NEWS_Cont"].ToString().Trim();
            CX_NEWS_Sort.Text = MyDt.Rows[0]["CX_NEWS_Sort"].ToString().Trim();
            CX_NEWS_Description.Text = MyDt.Rows[0]["CX_NEWS_Description"].ToString().Trim();
            CX_NEWS_KeyWords.Text = MyDt.Rows[0]["CX_NEWS_KeyWords"].ToString().Trim();
            CX_NEWS_Title.Text = MyDt.Rows[0]["CX_NEWS_Title"].ToString().Trim();
        }
    }

    /// <summary>
    /// 保存数据
    /// </summary>
    void PageSave()
    {
        DataTable MyDt = new DataTable();
        string CX_NEWS_Class_id = "";
        //看看是不是有选中 CX_NEWS_Class_id
        try
        {
            CX_NEWS_Class_id = Request.Form["CX_NEWS_Class_id"].Trim().Replace("'", "").Replace("\"", "");
        }
        catch
        {
            TextData.Err("程序不能完成你的添加新闻操作<br/>您没有选择所要添加新闻的分类");
        }

        if (CX_NEWS_Cont.Html.Trim() != "" && int.Parse(CX_NEWS_Sort.Text.Trim()) <= 99999 && CX_NEWS_Name.Text.Trim() != "" && CX_NEWS_Class_id != "")
        {
            try
            {
                id = Request.QueryString["id"].Trim();
                int.Parse(id);

            }
            catch
            {
                Response.Write("");
                Response.End();
            }
            string sql = "";

            string ISpicNews = "0";//是不是显示为图片新闻
            string ISTOpNews = "0"; //是不是显示为头条新闻
            if (CX_NEWS_Top.Checked == true)
            {
                ISTOpNews = "1";
            }

            if (CX_NEWS_Pic.Checked == true)
            {
                ISpicNews = "1";
            }

            sql = "Update  [CX_NEWS] Set ";
            sql = sql + " CX_NEWS_Title='" + CX_NEWS_Title.Text.Trim().Replace("'", "") + "',CX_NEWS_PublicTime='"+DateTime.Now.ToString()+"',CX_NEWS_Description='" + CX_NEWS_Description.Text.Trim().Replace("'", "") + "',CX_NEWS_KeyWords='" + CX_NEWS_KeyWords.Text.Trim().Replace("'", "") + "', Cx_keyWord_Name='" +
                //Cx_keyWord_Name.Text.Trim().Replace("'","").Replace("\"","") 病种关键字功能屏蔽 2007-10-11
                //+
                "', CX_NEWS_Name = '" + CX_NEWS_Name.Text.Trim() + "',CX_NEWS_Class_id = " + CX_NEWS_Class_id + ",CX_NEWS_Creater = '" + CX_NEWS_Creater.Text.Trim() + "',CX_NEWS_From = '" + CX_NEWS_From.Text.Trim() + "',CX_NEWS_Top = '" + ISTOpNews + "',CX_NEWS_Pic = '" + ISpicNews + "',CX_NEWS_Pic_Url = '" + CX_NEWS_Pic_Url.Text.Trim() + "',CX_NEWS_Cont = '" + CX_NEWS_Cont.Html.Trim() + "',CX_NEWS_Sort= " + CX_NEWS_Sort.Text.Trim() + ",CX_NEWS_Class_Edit='" + CX_NEWS_Class_Edit.Text.Trim() + "' where CX_NEWS_id=" + id;
            TextData.SqlDataSave(sql);
            TextThisSite.AddNewCX_loginState(Request.Cookies["admin"].Value, "CX_News_Edit.aspx", "修改新闻" + CX_NEWS_Name.Text + "");
            string xxsstt = CrtFile.Create_News_POintNewsID(id).Replace(Server.MapPath("/"), "/");
            tt.Text = "修改新闻 " + CX_NEWS_Name.Text + " 成功<br/>并以成功生成 <a href=\"" + xxsstt + "\" target=\"_blank\"> 点击查看" + xxsstt + "</a>";
            Response.Write(js.JsClose("成功修改！点击关闭!!!"));
            //Response.End();
            //tt.Text = "修改新闻 " + CX_NEWS_Name.Text + " 成功";
            
            //PageLand_First();
        }
        else
        {
            tt.Text = "新闻内容为空或其它内容为空";
        }
    }
    ///用于提示重复的信息 返回多个重复的连接
   [AjaxPro.AjaxMethod]
   public string ShowRepeat(string PubTitle,string PubId)
    {
        DataTable MyDt = new DataTable();
        string ReturnContent = "" ;
        MyDt = TextData.SqlataSet("select CX_NEWS_id from CX_NEWS where CX_NEWS_Name='" + PubTitle + "' and CX_NEWS_id <>" + PubId).Tables[0];
        if(MyDt.Rows.Count > 0)
        {
            for(int i = 0 ; i < MyDt.Rows.Count ; i++)
            {
                ReturnContent += "<input name='ChRepeat' type='checkbox' id='ChRepeat' value='" + MyDt.Rows[i]["CX_NEWS_id"].ToString() + "' /><a href='priview.aspx?id=" + MyDt.Rows[i]["CX_NEWS_id"].ToString() + "' target='_blank'>预览</a>";
            }
            ReturnContent = "<span id=spRepeat style=background:red;color:white;>重复内容</span>" + ReturnContent + "&nbsp;&nbsp;勾选提交后就可删除";//"alert('系统检测到该条信息的标题与数据库中已有的 【" + MyDt.Rows.Count.ToString() + "】 条标题重复！！');\r\n"+
        }
        return ReturnContent;
    }

}
